/*
 * SyncResource.java
 *
 * Created on April 12, 2007, 1:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.admin;

import org.atomojo.app.App;
import org.atomojo.app.client.XMLRepresentationParser;
import org.atomojo.app.db.DB;
import org.atomojo.app.db.RemoteApp;
import org.infoset.xml.Document;
import org.infoset.xml.util.DocumentDestination;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class RemoteAppResource extends ServerResource implements AdminXML
{
   
   DB db;
   String name;
   
   /** Creates a new instance of SyncResource */
   public RemoteAppResource() {
      setNegotiated(false);
   }
   
   public Representation get()
   {
      final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
      final String name = getRequest().getAttributes().get("name").toString();
      RemoteApp app = new RemoteApp(db,name);
      if (app.exists()) {
         return app.getRepresentation();
      } else {
         getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
         return null;
      }
   }
   
   public Representation put(Representation entity)
   {
      if (!XMLRepresentationParser.isXML(entity.getMediaType())) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Non-XML media type for entity body: "+entity.getMediaType().getName());
      }
      final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
      final String name = getRequest().getAttributes().get("name").toString();
      XMLRepresentationParser parser = new XMLRepresentationParser();
      Document doc = null;
      
      try {
         DocumentDestination dest = new DocumentDestination();
         parser.parse(entity,AdminApplication.createAdminDocumentDestination(dest,AdminXML.NM_APP));
         doc = dest.getDocument();
         RemoteApp app = new RemoteApp(db,doc.getDocumentElement());
         String lname = app.getName();
         if (!lname.equals(name)) {
            getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED);
            return new StringRepresentation("Cannot change the name of the remote app.");
         }
         if (app.exists()) {
            if (app.update()) {
               getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
               return null;
            } else {
               getContext().getLogger().severe("Cannot store XML for remote app.");
               getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
               return new StringRepresentation("Internal error, see logs.");
            }
         } else {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return null;
         }
      } catch (Exception ex) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("XML parse error: "+ex.getMessage());
      }
   }
   
   public Representation delete() {
      final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
      final String name = getRequest().getAttributes().get("name").toString();
      RemoteApp app = new RemoteApp(db,name);
      if (app.exists()) {
         if (app.delete()) {
            getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
         } else {
            getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         }
      } else {
         getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
      }
      return null;
   }
   
}
